""" ContactsDirectory 1.0 to 2.0 migration script. 
    To run the migration, add a new External Method to your Plone site with the following parameters:
    id: migrateCD
    Title: Migrate ContactsDirectory
    Module Name: ContactsDirectory.migrate1dot0to2dot0
    Function Name: migrate
    
    To run the migration, either click the "Test" tab of the External Method in the ZMI, or call it through your browser at http://yourplonesiturl/pathtoscript/migrateCD. """

from Products.CMFCore.utils import getToolByName
from StringIO import StringIO
try:
    from Products.ATContentTypes.migration.walker import CatalogWalker
    from Products.ATContentTypes.migration.migrator import CMFFolderMigrator, CMFItemMigrator
except ImportError:
    try:
        from Products.contentmigration.basemigrator.walker import CatalogWalker
        from Products.contentmigration.basemigrator.migrator import CMFItemMigrator
    except ImportError:
        raise ImportError, "Unable to find product: contentmigration. Contacts Directory migration could not be completed."
from Products.ContactsDirectory.Extensions.Install import linkableKupuTypes, mediaKupuTypes, collectionKupuTypes, addKupuResource, removeKupuResource

        
class ContactsDirectoryMigrator(CMFItemMigrator):
    walkerClass = CatalogWalker
    src_meta_type = 'ContactsDirectory'
    src_portal_type = 'ContactsDirectory'
    dst_meta_type = 'CDContactsDirectory'
    dst_portal_type = 'CDContactsDirectory'

class PersonMigrator(CMFItemMigrator):
    walkerClass = CatalogWalker
    src_meta_type = 'Person'
    src_portal_type = 'Person'
    dst_meta_type = 'CDPerson'
    dst_portal_type = 'CDPerson'

class ClassificationMigrator(CMFItemMigrator):
    walkerClass = CatalogWalker
    src_meta_type = 'Classification'
    src_portal_type = 'Classification'
    dst_meta_type = 'CDClassification'
    dst_portal_type = 'CDClassification'
   
class CollectionMigrator(CMFItemMigrator):
    walkerClass = CatalogWalker
    src_meta_type = 'Collection'
    src_portal_type = 'Collection'
    dst_meta_type = 'CDCollection'
    dst_portal_type = 'CDCollection'
    
class OrganizationsFolderMigrator(CMFItemMigrator):
    walkerClass = CatalogWalker
    src_meta_type = 'OrganizationsFolder'
    src_portal_type = 'OrganizationsFolder'
    dst_meta_type = 'CDOrganizationsFolder'
    dst_portal_type = 'CDOrganizationsFolder'

class OrganizationMigrator(CMFItemMigrator):
    walkerClass = CatalogWalker
    src_meta_type = 'Organization'
    src_portal_type = 'Organization'
    dst_meta_type = 'CDOrganization'
    dst_portal_type = 'CDOrganization'

class GroupMigrator(CMFItemMigrator):
    walkerClass = CatalogWalker
    src_meta_type = 'Group'
    src_portal_type = 'Group'
    dst_meta_type = 'CDGroup'
    dst_portal_type = 'CDGroup'

class GroupsFolderMigrator(CMFItemMigrator):
    walkerClass = CatalogWalker
    src_meta_type = 'GroupsFolder'
    src_portal_type = 'GroupsFolder'
    dst_meta_type = 'CDGroupsFolder'
    dst_portal_type = 'CDGroupsFolder'

class PersonGroupingMigrator(CMFItemMigrator):
    walkerClass = CatalogWalker
    src_meta_type = 'PersonGrouping'
    src_portal_type = 'PersonGrouping'
    dst_meta_type = 'CDPersonGrouping'
    dst_portal_type = 'CDPersonGrouping'

class ProvinceMigrator(CMFItemMigrator):
    walkerClass = CatalogWalker
    src_meta_type = 'Province'
    src_portal_type = 'Province'
    dst_meta_type = 'CDProvince'
    dst_portal_type = 'CDProvince'

class OrganizationMembershipMigrator(CMFItemMigrator):
    walkerClass = CatalogWalker
    src_meta_type = 'OrganizationMembership'
    src_portal_type = 'OrganizationMembership'
    dst_meta_type = 'CDOrganizationMembership'
    dst_portal_type = 'CDOrganizationMembership'

class GroupInformationMigrator(CMFItemMigrator):
    walkerClass = CatalogWalker
    src_meta_type = 'GroupInformation'
    src_portal_type = 'GroupInformation'
    dst_meta_type = 'CDGroupInformation'
    dst_portal_type = 'CDGroupInformation'

class ProvincialMembershipMigrator(CMFItemMigrator):
    walkerClass = CatalogWalker
    src_meta_type = 'ProvincialMembership'
    src_portal_type = 'ProvincialMembership'
    dst_meta_type = 'CDProvincialMembership'
    dst_portal_type = 'CDProvincialMembership'

class ContactsDirectoryToolMigrator(CMFItemMigrator):
    walkerClass = CatalogWalker
    src_meta_type = 'ContactsDirectoryTool'
    src_portal_type = 'ContactsDirectoryTool'
    dst_meta_type = 'CDContactsDirectoryTool'
    dst_portal_type = 'CDContactsDirectoryTool'

def migrate(self):
    """Run the migration"""

    out = StringIO()
    print >> out, "Starting migration"
    
    print >> out, "Removing Kupu customizations"
    oldLinkableKupuTypes = ['Person', 'Collection', 'Classification', 'Province', 'Organization', 'OrganizationsFolder', 'Group', 'GroupsFolder']
    oldMediaKupuTypes = ['Person']
    oldCollectionKupuTypes = ['ContactsDirectory']

    for type in oldLinkableKupuTypes:
        removeKupuResource(self, 'linkable', type)
    for type in oldMediaKupuTypes:
        removeKupuResource(self, 'mediaobject', type)
    for type in oldCollectionKupuTypes:
        removeKupuResource(self, 'collection', type)

    portal_url = getToolByName(self, 'portal_url')
    portal = portal_url.getPortalObject()
    cat = getToolByName(self, 'portal_catalog')
  
    migrators = ContactsDirectoryMigrator, ClassificationMigrator, PersonMigrator, CollectionMigrator, OrganizationsFolderMigrator, OrganizationMigrator, GroupMigrator, GroupsFolderMigrator, PersonGroupingMigrator, ProvinceMigrator, OrganizationMembershipMigrator, GroupInformationMigrator, ProvincialMembershipMigrator, ContactsDirectoryToolMigrator
  
    for migrator in migrators:
        walker = migrator.walkerClass(portal, migrator)
        print >> out, "Migrating %s..." % migrator.src_portal_type
        walker.go(out=out)
        print >> out, walker.getOutput()
        # Update the portal_type since the migrator doesn't seem to be doing it. Jackass.
        results = cat(portal_type=migrator.src_portal_type)
        for brain in results:
            ob = brain.getObject()
            ob._setPortalTypeName(migrator.dst_portal_type)
            ob.reindexObject(['portal_type'])
  
    # Force an update of person images so that member portraits are set.    
    print >> out, "Updating member portraits"
    results = cat(portal_type="CDPerson")
    for brain in results:
        person = brain.getObject() 
        person.setImage(person.getImage())
      
    # And let's also update workflows
    print >> out, "Updating workflow"
    wf = getToolByName(self, 'portal_workflow')
    wf.updateRoleMappings()

    # Update Kupu settings
    print >> out, "Applying Kupu customizations"
    for type in linkableKupuTypes:
        addKupuResource(self, 'linkable', type)
    for type in mediaKupuTypes:
        addKupuResource(self, 'mediaobject', type)        
    for type in collectionKupuTypes:
        addKupuResource(self, 'collection', type)
    
     
    print >> out, "Migration finished"
    return out.getvalue()